package com.freez.spark.core.operation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * FREEDOM  2021 人生苦短，不妨一试
 *
 * @Classname: CountAndReduce.java
 * @Author: zcs
 * @Date: 2021年-12月-08日 周三 11:13
 * @Description: 统计，逻辑运算类算子
 */
public class CountAndReduce {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setAppName("SparkCore-CountAndReduce").setMaster("local");
        JavaSparkContext jsc = new JavaSparkContext(conf);
//数据
        List<Tuple2<String, Integer>> dataList = new ArrayList<>();
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 11));
        dataList.add(new Tuple2<String, Integer>("2020-05-02", 4));
        dataList.add(new Tuple2<String, Integer>("2020-05-02", 7));
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 4));
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 10));
        JavaPairRDD<String, Integer> aRdd = jsc.parallelizePairs(dataList);
        JavaRDD<String> bRdd = jsc.parallelize(Arrays.asList("hello world", "hello hi"));
        /**
         * count count()为统计RDD中的元素个数，返回值为long类型
         */
        JavaRDD<String> oRdd = jsc.parallelize(Arrays.asList("hello world", "hello hi", "hello jack"));
        System.out.println(oRdd.count());
        /**
         * countByKey countByKey()为按照相同key进行统计，最后返回Map<k,v>，</br>
         * 其中key代表RDD元素中的key，而value则代表这些相同key的个数，且其类型为long类型
         */
        Map<String, Long> map = aRdd.countByKey();
        map.forEach((key, value) -> System.out.println(key + value));
        /**
         * reduce reduceByKey reduceByKey()是按照相同的key值进行归并(加减等运算)运算。
         */
        JavaPairRDD<String, Integer> reduceByKey = aRdd.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v2 - v1;
            }
        });
        reduceByKey.foreach((VoidFunction<Tuple2<String, Integer>>) s -> System.out.println(s));

        jsc.close();

    }
}